#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time    : 2021/12/19 15:19
# @Author  : Ryu
# @Site    : 
# @File    : appWindows.py
# @Software: PyCharm
import os
from flask import Flask, jsonify, make_response, send_file
from flask import request
from flask import render_template
import requests
from flask_cors import *
import json
import re
import time
import threading
import pymysql
import pandas as pd

user_type_dict = {-1: '无认证', 0: '个人账户', 3: '非个人用户'}  # 个人：个人账户仍保留橙色黄色标识，非个人用户(机构、企业、媒体等)将采用全新蓝色标识。

app = Flask(__name__)
CORS(app, spports_credentials=True)

conn = pymysql.connect(host='139.180.204.4', user="lening", passwd="123456", port=3306,
                       db="testDataBase")
cur = conn.cursor()
print("数据库连接成功")
listAll = []


def initDataList():
    listAll.append([])
    listAll.append([])
    listAll.append(1)
    listAll.append(1)
    listAll.append(50)
    listAll.append("")
    listAll.append(0)
    listAll.append(False)
    listAll.append(False)
    listAll.append(False)
    listAll.append(1)
    listAll.append(2)
    listAll.append([])
    listAll.append("")


initDataList()

"""
#0#  all_data = []
#1#  onePageData = []
#2#  pageTemp = 0
#3#  pageStart = 1
#4#  page = 20
#5#  keyword = ""
#6#  pageNow = 0
#7#  overRequest = False
#8#  stopRequest = False
#9#  starRequest = False
#10# dataSum = 0
#11# sleepTime = 1
#12# downData = all_date
#13# downKeyword = ""
"""


def ScaryData():
    print("正在爬取")
    for i in range(listAll[3] + 0, listAll[4] + 1):
        listAll[2] = i
        try:
            print("正在爬取第" + str(i) + "页")
            listAll[6] = str(i)
            get_one_page()
        except:
            print("内容含有未知字符或文本太长，插入数据库失败")
            continue
        while len(listAll[0]) >= 10:
            listAll[8] = True
    listAll[7] = True


def wait():
    while listAll[8]:
        if len(listAll[0]) < 2:
            listAll[8] = False
        time.sleep(1)
        print("Scary Already Stop!")


def get_one_page():
    print("开始响应")
    params = {
        'containerid': f'100103type=1&q={listAll[5]}',
        'page_type': 'searchall',
        'page': str(listAll[6])
    }
    url = 'https://m.weibo.cn/api/container/getIndex?'  # 请求api
    response = requests.get(url, params=params).text
    id_ls = re.findall('"id":"(.{16}?)",', response, re.DOTALL)
    detail_url = ['https://m.weibo.cn/detail/' + i for i in id_ls]
    for i in detail_url:
        wait()
        response = requests.get(i).text
        print("响应成功")
        # print(response)
        data = re.findall("var \$render_data = \[({.*})]\[0]", response, re.DOTALL)[0]
        data = json.loads(data)['status']
        # 发文时间
        created_at_time = data['created_at']
        timeArray = time.strptime(created_at_time, "%a %b %d %H:%M:%S +0800 %Y")
        created_at_time = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
        # 文章id
        log_id = data['id']
        # 文章内容
        log_text = data['text']
        log_text = re.sub('<.*?>', '', log_text)
        # 发文设备
        source = data["source"]
        # 转发数"reports_count": 1376,
        reports_count = data['reposts_count']
        # 评论数"comments_count": 10244,
        comments_count = data['comments_count']
        # 点赞数"attitudes_count": 326440
        attitudes_count = data['attitudes_count']
        # 用户id-->"id": 1951123110,
        user_id = data['user']['id']
        # 用户昵称
        user_screen_name = data['user']['screen_name']
        # 用户验证类型-->"verified_type": 3,
        user_type = data['user']['verified_type']

        if user_type not in user_type_dict:
            continue
        user_type = user_type_dict[user_type]
        Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        one_piece_data = (user_id, created_at_time, i, log_text, source, reports_count,
                          comments_count, attitudes_count, user_screen_name, user_type, Time, listAll[5])
        column_name = ('user_id', 'created_at_time', 'title_address', 'title_content', 'phone_source', 'forward_count',
                       'comment_count', 'attitudes_count', 'user_screen_name', 'user_type', 'scary_date', 'keyword')
        print("开始打印第" + listAll[6] + "页数据")
        print(one_piece_data)
        insert_sql = "insert into Vibo(user_id,created_at_time,title_address,title_content,phone_source,forward_count ,comment_count,attitudes_count,user_screen_name,user_type,scary_date,keyword )VALUES( '{0}', '{1}','{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}','{10}','{11}')".format(
            user_id, created_at_time, i, log_text, source, reports_count, comments_count, attitudes_count,
            user_screen_name, user_type, Time, listAll[5])
        print(Time)
        print("开始装入数据库")
        cur.execute(insert_sql)
        conn.commit()
        listAll[10] = listAll[10] + 1
        print("insert succeed!")
        listAll[0].append(dict(zip(column_name, one_piece_data)))
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
        time.sleep(listAll[11])
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
        if listAll[7]:
            print('ScarySinaOver')
            return 'ScarySinaOver'


@app.route('/', methods=['GET', 'POST'])
def home():
    return render_template('login.html')  # 返回home.html模板


@app.route('/login', methods=['GET', 'POST'])
def Login():
    Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

    if request.json['username'] == 'Lening' and request.json['password'] == 'admin':
        print(Time + "Login SUCCEED")
        return {'isPermit': True}
    else:
        print()
        print(Time + "Login FAIL")
        return {'isPermit': False}


@app.route('/ScarySina', methods=['GET', 'POST'])
def ScarySina():
    listAll[5] = request.json['keyword']
    print("开始爬取话题：" + listAll[5])
    return '开始爬取'


@app.route("/over", methods=['GET', 'POST'])
def me_api():
    listAll[7] = True
    return 'Scary over!'


@app.route("/getOne", methods=['GET', 'POST'])
def getOne():
    if not listAll[9]:
        listAll[9] = True
        thread = threading.Thread(target=ScaryData)
        thread.start()
    if len(listAll[0]) == 0:
        print(listAll[0])
        print(listAll[1])
        listAll[8] = False
        return 'NULL'
    else:
        data = listAll[0].pop(0)
        print("当前listAll长度")
        print(len(listAll[0]))
        return data


@app.route("/queryKeyword", methods=['GET', 'POST'])
def queryByKeyword():
    keyword = request.args.get('keyword')
    listAll[13] = keyword
    sql = "select * from Vibo where keyword = '{0}'".format(keyword)
    cur.execute(sql)
    row = cur.fetchall()
    data = []
    for i in row:
        one_piece_data = (i[1], i[2], i[3], i[4], i[5], i[6],
                          i[7], i[8], i[9], i[10], i[11], i[12])
        column_name = ('user_id', 'created_at_time', 'title_address', 'title_content', 'phone_source', 'forward_count',
                       'comment_count', 'attitudes_count', 'user_screen_name', 'user_type', 'scary_date', 'keyword')
        column_name_down = ('用户ID', '发文日期', '微博地址', '文章内容', '手机类型', '转发数',
                            '评论数', '点赞数', '用户昵称', '认证类型', '爬取日期', '关键词')
        data.append(dict(zip(column_name, one_piece_data)))
        listAll[12].append(dict(zip(column_name_down, one_piece_data)))
    print(listAll[12])
    # return jsonify(data)
    return {
        "data": data
    }


@app.route('/down')
def down():
    print(listAll[12])
    file_name = listAll[13]
    df = pd.DataFrame(listAll[12])
    df.to_excel('./download/{}.xlsx'.format(file_name), index=False)
    UPLOAD_FOLDER = './download/{}.xlsx'.format(file_name)
    ROOT_FOLDER = os.path.join(os.getcwd(), UPLOAD_FOLDER)  # 整合绝对路径
    response = make_response(send_file(ROOT_FOLDER, mimetype='xlsx/csv', attachment_filename='Adjacency.csv'))
    return response


if __name__ == '__main__':
    app.run()
    """--host=192.168.43.253 --port=5000"""
